<template>
	<vk-data-drawer
		v-model="value.show"
		:title="page.title"
		:top="page.top"
		:width="page.width"
		:close-on-click-modal="true"
		mode="form"
		@open="onOpen"
		@closed="onClose"
	>
		<!-- 页面主体内容开始 -->
		<vk-data-form
			ref="form1"
			v-model="form1.data"
			:form-type="value.mode"
			:rules="form1.props.rules"
			:action="form1.props.action"
			:columns="form1.props.columns"
			:loading.sync="form1.props.loading"
			:labelWidth="form1.props.labelWidth"
			:before-action="form1.props.beforeAction"
			:show-cancel="page.showCancel"
			:cancel-text="page.cancelText"
			:submit-text="page.submitText"
			max-height="1000px"
			@success="onFormSuccess"
		>
		</vk-data-form>
		<!-- 页面主体内容结束 -->
	</vk-data-drawer>
</template>

<script>
var that; // 当前页面对象
var vk = uni.vk; // vk实例
// 表单默认值
var formDefaultValue = {
	is_custom:0,
	regional_reward_multiple:1,
	share_reward_1:{
		is_calc:true,
		is_fixed_calc:false,
		proportion_value:"",
		fixed_value:"",
		is_level_k: true
	},
	share_reward_2:{
		is_calc:true,
		is_fixed_calc:false,
		proportion_value:"",
		fixed_value:"",
		is_level_k: true
	}
};
export default {
	props: {
		value: {
			Type: Object,
			default: function() {
				return {
					show: false,
					mode: "",
					item: {}
				};
			}
		}
	},
	data: function() {
		// 组件创建时,进行数据初始化
		return {
			page: {
				title: "设置该商品奖励应用此单独规则",
				submitText: "设置",
				cancelText: "关闭",
				showCancel: true,
				top: "0.5vh",
				width: "1100px"
			},
			form1: {
				// 表单请求数据，此处可以设置默认值
				data: {
					tableList:[]
				},
				// 表单属性
				props: {
					// 表单请求地址
					action: "admin/business/sys/goods.updateRewardRule",
					// 表单字段显示规则
					columns: [
						{ key: "is_custom", title: "是否开启自定义", type: "radio",
							optionType:"button",
							data: [
								{ value: 0, label: "使用默认规则" },
								{ value: 1, label: "开启自定义" },
								{ value: -1, label: "关闭奖励" },
							]
						},
						{ key:"regional_reward_multiple", title:"区域奖励倍数", type:"number", placeholder:"输入倍数", width:160, clearable:false, max:100, rightText:"倍", showRule:"is_custom=1" },
						{
						  key:"tableList", title:"分销奖励", type:"array<object>", showLabel: false,
						  showAdd:false,
						  showClear:false,
						  showSort:false,
							rowNo:false,
							rowHeight:90,
						  rightBtns:[],
						  // 每行每个字段对应的渲染规则
						  columns:[
								{ key:"_index", title:"奖励类型", type:"text-view", minWidth:140 },
								{ key:"is_calc", title:"是否有奖励", type:"switch", width:140 },
								{ key:"is_fixed_calc", title:"奖励类型", type:"radio", minWidth:200, size:"small",
									optionType:"button",
									data: [
										{ value: false, label: "百分比" },
										{ value: true, label: "固定值" },
									],
									showRule:"is_calc=true" ,
								},
								{ key:"proportion_value", title:"百分比", type:"percentage", clearable:false, max:100, minWidth:200, showRule:"is_calc==true&&is_fixed_calc==false" },
								{ key:"fixed_value", title:"固定值", type:"money", placeholder:"请输入固定奖励", minWidth:200, rightText:"元", showRule:"is_calc==true&&is_fixed_calc==true" },
								{ key:"is_level_k", title:"是否受分销等级影响", type:"switch", width:150 },
							],
							showRule:"fx_mode==fx2&&is_custom==1"
						}
					],
					// 表单验证规则
					rules: {
						regional_reward_multiple: [
							// 必填
							{ required: true, message: "区域奖励倍数不能为空", trigger: ['blur','change'] }
						],
					},
					// 左侧label的宽度
					labelWidth: "120px",
					beforeAction:(formData)=>{
						// 可在此处修改 formData 后返回 formData，若在此处return false，则表单不触发提交请求。
						return {
							_id: formData._id,
							reward_rule:{
								is_custom: formData.is_custom,
								regional_reward_multiple: formData.regional_reward_multiple,
								share_reward_1: formData.tableList[0],
								share_reward_2: formData.tableList[1],
							}
						};
					},

				}
			}
		};
	},
	mounted() {
		that = this;
		that.init();
	},
	methods: {
		// 初始化
		init() {
			let { value } = that;
			that.$emit("input", value);
		},
		// 监听 - 页面打开
		onOpen() {
			that = this;
			let { item={} } = that.value;
			// 每次打开时，重新设置表单的值 = value.item 的值，item通过 vk.pubfn.openForm('表单名',{ item:{ _id:"1" } }) 传递值
			let { reward_rule={} } = item;
			let fx_mode = vk.getVuex("$user.mchInfo.fx.mode");

			that.form1.data = vk.pubfn.objectAssign(formDefaultValue, {
				...reward_rule,
				_id:item._id,
				fx_mode,
			});
			that.$set(that.form1.data,"tableList",[
				{ ...that.form1.data.share_reward_1, _index:"一级分销奖励"},
				{ ...that.form1.data.share_reward_2, _index:"二级分销奖励"},
			]);
			that.keyChange();
		},
		keyChange(){

		},
		// 监听 - 页面关闭
		onClose() {
			//that.$refs.form1.resetForm(); // 关闭时，重置表单
		},
		// 监听 - 提交成功后
		onFormSuccess({ data }) {
			vk.pubfn.objectAssignForVue(that.value.item, data.info, that);
			that.value.show = false; // 关闭页面
			that.$emit("success");
		}
	},
	watch: {


	},
	// 计算属性
	computed: {

	}
};
</script>

<style lang="scss" scoped></style>
